<?php
class SettlementAction extends CommonAction {
	

	/**
	 * 报名单结算
	 */
	public function settlementOrder(){
		$result = false;
		$Order = D ( "Order" );
		$ids = $_POST['ids'];		
		$arrayIds = explode(',',$ids);
		
// 		$data["DD_DDZT"]  = 'DDZT3'; // 设置为已结算状态   
		$countJe =0;
		$ddbhs ="";
		$Order->startTrans();
		foreach ($arrayIds as $id){
// 			$result = $Order->where('Id='.$id)->save($data);
			$result = $Order->where('Id='.$id)->setField(array('DD_DDZT'=> 'DDZT3','DD_JSR'=>$_SESSION['loginUserId'],'DD_JSR_ID'=>$_SESSION['loginUserName']));
			
			$ddbh = $Order->where('Id='.$id)->field('DD_BH,DD_SF')->select(); //获取订单编号
			$OrderState = A('OrderState'); //实例订单跟踪action
			$result = $OrderState->saveDDGZ($ddbh[0]['DD_BH'],'DDZT3',$_SESSION['loginUserId'],$_SESSION['loginUserName']); //添加订单跟踪记录
			$countJe += $ddbh[0]['DD_SF'];//累加实付金额
			$ddbhs = $ddbhs . "," . $ddbh[0]['DD_BH'];
		}
		if($countJe>0 && $result){
			$data = array();
			$account= D('Account');
			$data["ZM_LS"] = $account->getAccountLSBH($_SESSION['xqid'],date("Ymd"));
			$data["ZM_XQ"] = $_SESSION['xqid'];
			$data["ZM_CLRID"] = $_SESSION['loginUserId'];
			$data["ZM_CLR"] = $_SESSION['loginUserName'];
			$data["ZM_CLSJ"] = date("Y-m-d H:i:s");
			$data["ZM_XMSM"] = "自动结算的入账操作";
			$data["ZM_LB"] = "ZMLB3";
			$data["ZM_JE"] = $countJe;
			$data["ZM_CZLX"] = "ZMCZLX1";
			$data["ZM_BZ"] = $ddbhs;
			$data["status"] = "1";
			$result = $account->add($data);
		}
		if($result){
			$Order->commit();
		}else{
			$Order->rollback();
		}
// 		$pk = $Order->getPk();
// 		$condition = array("id" => array('in', explode(',', $ids)));
// 		$result = $Order->where($condition)->setField('DD_DDZT', 'DDZT3');
		if ($result == false) {
			echo json_encode (array(
					'msg' => '结算出错！'
			));
		} else {
			echo json_encode (array(
					'success' => true
			));
		}
	}
	
	/**
	 * 报名单打印
	 */
	public function printOrder(){
		$Order = D ( "Order" );
		$id = $_POST['orderID'];
	
		$orderInfo = $Order->where('Id='.$id)->select(); //获取订单信息
		$orderJson = json_encode($orderInfo[0]);
			
		$OrderListObject = D("OrderList");
		$courseDetail = $OrderListObject->where('LS_DDBH ="'.$orderInfo[0]['DD_BH'].'"')->select();//获取该订单下所有课程
			
		$courseJson = json_encode($courseDetail);
	
		$orderJson = substr($orderJson, 0, strlen($resultJson)-1);
	
	
		$json = $orderJson . ',"courses":' . $courseJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	/**
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read()
	 */
	public function readOrder() {
		if(!empty($_POST['DD_DDZT'])) {
			$map = $map." and dd.DD_DDZT = '".$_POST['DD_DDZT']."'";
		}else{
			$map = " and (dd.DD_DDZT = 'DDZT2' or dd.DD_DDZT = 'DDZT3') ";		//显示为已缴费和已结算
		}
				
		if(!empty($_POST['DD_BH'])) {
			$map = $map." and dd.DD_BH = '".$_POST['DD_BH']."'";
		}
		if(!empty($_POST['DD_KHBH'])) {
			$map = $map." and dd.DD_KHBH = '".$_POST['DD_KHBH']."'";
		}
		if(!empty($_POST['DD_KHMC'])) {
			$map = $map ." and dd.DD_KHMC like '%".$_POST['DD_KHMC']."%'";
		}
		if(!empty($_POST['DD_PPMC'])) {
			$map = $map ." and dd.DD_PPMC like '%".$_POST['DD_PPMC']."%'";
		}
		
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$map = $map." and dd.DD_ZD in (".$xqid.") ";
		}
		
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		
		$Order = M ( "Order" );
		
		$arrNum = $Order->query("select count(*) as num from smis_order dd where 1=1 " . $map);
		$row = $arrNum[0]['num'];
		$total = $row;	

		$Orders = array ();
		$Orders = $Order->table('smis_order dd, smis_dictionary_list dictionary,smis_dictionary_list dictionary1,smis_dictionary_list dictionary2')
						->where('dd.DD_ZFFS = dictionary.ITEM_KEY and dd.DD_DDZT = dictionary1.ITEM_KEY and dd.DD_ZD = dictionary2.ITEM_KEY' .$map)
						->field('dd.*,dictionary.ITEM_VALUE as ZFFS,dictionary1.ITEM_VALUE as DDZT,dictionary2.ITEM_VALUE as ZD')
						->order('dd.DD_CJSJ desc' )
		                ->page($pagenum,$rowsnum)->select();
		
		$resultJson = json_encode($Orders);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	
	}
	
	/**获取课程列表信息
	 * 查询(non-PHPdoc)
	* @see CommonAction::read()
	* */
	public function getCourseList() {
	
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$course = D ( "Course" );
		$map = " and course.status ='1' " ;
	
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$map = $map." and course.KC_XQ in (".$xqid.") ";
		}
	
		$arrNum = $course->query("select count(*) as num from smis_course dd where 1=1 " . $map);
		$row = $arrNum[0]['num'];
		$total = $row;
	
		$courseList = array ();
		$courseList = $course->table('smis_course course, smis_dictionary_list dictionary')
		->where('course.KC_KMBH = dictionary.ITEM_KEY'.$map)
		->field('course.*,dictionary.ITEM_VALUE as KMMC')
		->order('course.id desc' )
		->page($pagenum,$rowsnum)->select();
	
		$resultJson = json_encode($courseList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	
	/**
	 * 获取报名单流水明细
	 */
	public function  readOrderDetails(){
		if(!empty($_POST['LS_DDBH'])) {
			$map['LS_DDBH'] = $_POST['LS_DDBH'];
			$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
			$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
			$OrderDetails = M ( "OrderList" );
				
			$total = $OrderDetails->where($map)->count (); // 计算总数
			import("ORG.Util.Page");// 导入分页类
			$Orders = array ();
			$Orders = $OrderDetails->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
			$resultJson = json_encode($Orders);
			if($resultJson == "null"){//如果查询结果返回为null
				$resultJson = "[]";
			}
		}else {
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	
	/**
	 * 获取学生列表
	 */
	public function studentList() {
		$map =  array ();
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$Student = M ( "Student" );
	
		$xqid =  $_SESSION['XqFilterString'] ;//获取用户当前拥有校区权限
		if(!empty($xqid)){
			$xqid = str_replace("'","",$xqid);
			$map['XS_XQ']  = array('in',$xqid);
		}
	
		if(!empty($_POST['KHMC'])) {
			$KCMC = $_POST['KHMC'];
			$keyword = "%". $KCMC ."%";
			$where['_logic'] = 'or';
			$where['XS_XM'] = array('like',$keyword );
			$where['XS_PY'] = array('like',$keyword );
			$where['XS_XH'] = array('like',$keyword );
			$map['_complex'] = $where;
		}
	
		$map['XS_ZT'] = 'xszt1'; //表明活跃的学生
	
		$total = $Student->where($map)->count (); // 计算总数
		//$list = $db -> field( "$table.art_id,$table.art_title,$table.art_content,$table.art_description,$table2.cate_id,$table2.cate_name" ) ->
		$studentList = array ();
		$studentList = $Student->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($studentList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	/**
	 * 获取优惠劵列表
	 */
	public function couponList() {
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$Coupon = M ( "Coupon" );
		$fix = C( "DB_PREFIX" );
		import("ORG.Util.Page");// 导入分页类
			
		$nowDate = date("Y-m-d");
		$map['status'] = '1';
		$map['YHK_KSSJ']=array('elt',$nowDate);
		$map['YHK_JSSJ']=array('egt',$nowDate);
	
		$total = $Coupon->where($map)->count (); // 计算总数
		$couponList = array ();
		$couponList = $Coupon->where($map)->limit ( ($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($couponList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
		echo $json;
	}
	
	/**
	 * 订单收费状态
	 */
	public function getDDZT(){
		echo '[{"ITEM_KEY":"0","text":"请选择","selected":true},{"ITEM_KEY":"DDZT2","text":"已缴费"},{"ITEM_KEY":"DDZT3","text":"已结算"}]' ;
	}
}